package com.example.xingxing.mobilesafe.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xingxing.mobilesafe.R;
import com.example.xingxing.mobilesafe.activity.Part1.LostAndFindActivity;
import com.example.xingxing.mobilesafe.utils.MD5Utls;
import com.example.xingxing.mobilesafe.utils.PreferenceUtil;
import com.example.xingxing.mobilesafe.utils.ToastUtil;

import java.util.List;

public class HomeActivity extends Activity {

    private GridView gridView;
    private String[] gvDatas = new String[]{"手机防盗", "通讯卫士", "软件管理", "进程管理", "流量管理", "手机杀毒", "缓存清理", "高级工具", "设置中心"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        gridView = (GridView) findViewById(R.id.gv_home);
        gridView.setAdapter(new HomeGVApdater());
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i) {
                    case 0://手机防盗
                        showSafeDialog();
                        break;
                    case 8:// 设置中心
                        startActivity(new Intent(getApplicationContext(), SettingActivity.class));
                        break;
                    default:
                        break;
                }
            }
        });
    }

    /*
    *
    * 手机防盗弹窗
    *
    */
    protected void showSafeDialog() {
        if (PreferenceUtil.getString("password", null, getApplicationContext()) == null) {
            showSetPasswordDialog();
        } else  {
            showInputPasswordDialog();
        }

    }

    /*
     * 设置密码弹窗
     */
    protected void showSetPasswordDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_setup_password, null);
        dialog.setView(view,0,0,0,0);
        dialog.show();

        final EditText passwordEditText = (EditText) view.findViewById(R.id.password_textfield);
        final EditText confirmPasswordEditText = (EditText) view.findViewById(R.id.confirm_password_textfield);


        Button confirmButton = (Button) view.findViewById(R.id.confirm_button);
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String confirmPassword = confirmPasswordEditText.getText().toString().trim();
                String password = passwordEditText.getText().toString().trim();
                if (TextUtils.isEmpty(password) || TextUtils.isEmpty(confirmPassword)) {
                    ToastUtil.showToast(getApplicationContext(), "输入内容不能为空");
                } else if (password.equals(confirmPassword)) {
                    ToastUtil.showToast(getApplicationContext(), "设置成功");
                    PreferenceUtil.putString("password", password, getApplicationContext());
                    dialog.dismiss();
                } else  {
                    ToastUtil.showToast(getApplicationContext(), "两次密码输入不一致！请重新输入");
                }
            }
        });

        Button cancelButton = (Button) view.findViewById(R.id.cancel_button);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
    }

    /*
     * 输入密码弹窗
     */
    protected void showInputPasswordDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_input_password, null);
        dialog.setView(view,0,0,0,0);
        dialog.show();

        final EditText passwordEditText = (EditText) view.findViewById(R.id.password_textfield);


        Button confirmButton = (Button) view.findViewById(R.id.confirm_button);
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String password = passwordEditText.getText().toString().trim();
                if (TextUtils.isEmpty(password)) {
                    ToastUtil.showToast(getApplicationContext(), "请输入密码");
                } else if (MD5Utls.encode(password).equals(MD5Utls.encode(PreferenceUtil.getString("password", null, getApplicationContext())))){
                    startActivity(new Intent(getApplicationContext(), LostAndFindActivity.class));
                    dialog.dismiss();
                } else {
                    ToastUtil.showToast(getApplicationContext(), "密码错误 ");
                }
            }
        });

        Button cancelButton = (Button) view.findViewById(R.id.cancel_button);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
    }

    class HomeGVApdater extends BaseAdapter {

        @Override
        public int getCount() {

            return gvDatas.length;
        }

        @Override
        public Object getItem(int i) {

            return null;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
           View localView =  View.inflate(getApplicationContext(), R.layout.list_item_homegv, null);
            TextView textView = (TextView) localView.findViewById(R.id.gv_home_title);
            textView.setText(gvDatas[i]);
            return localView;
        }
    }
}
